package org.example.flea_market_mvc.service.orders.impl;

import org.example.flea_market_mvc.entity.Orders;
import org.example.flea_market_mvc.entity.ShoppingCarts;
import org.example.flea_market_mvc.entity.Products;
import org.example.flea_market_mvc.mapper.OrdersMapper;
import org.example.flea_market_mvc.mapper.ShoppingCartsMapper;
import org.example.flea_market_mvc.mapper.ProductsMapper;
import org.example.flea_market_mvc.param.OrderParam;
import org.example.flea_market_mvc.service.orders.OrdersService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 订单服务实现类
 * 处理订单创建、状态更新、查询等核心业务逻辑
 */
@Service
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private ShoppingCartsMapper shoppingCartsMapper;
    @Autowired
    private ProductsMapper productsMapper;

    @Override
    public int insert(OrderParam orderParam) {
        Orders order = new Orders();
        BeanUtils.copyProperties(orderParam, order);
        return ordersMapper.insert(order);
    }

    @Override
    public int deleteById(Long id) {
        return ordersMapper.deleteById(id);
    }

    @Override
    public int update(OrderParam orderParam) {
        Orders order = new Orders();
        BeanUtils.copyProperties(orderParam, order);
        return ordersMapper.update(order);
    }

    @Override
    public Orders selectById(Long id) {
        return ordersMapper.selectById(id);
    }

    @Override
    public List<Orders> selectAll() {
        return ordersMapper.selectAll();
    }
    @Override
    public List<Orders> selectByUserId(Long id) {return ordersMapper.selectByUserId(id);}

    @Override
    @Transactional
    public boolean checkout(Integer userId, List<Integer> cartItemIds) {
        // 查询购物车项
        List<ShoppingCarts> cartItems = shoppingCartsMapper.selectBatchIds(cartItemIds);
        if (cartItems == null || cartItems.isEmpty()) return false;
        for (ShoppingCarts item : cartItems) {
            Products product = productsMapper.selectById(item.getProductId());
            if (product == null || product.getStock() < item.getQuantity()) {
                throw new RuntimeException("商品库存不足或不存在");
            }
            // 扣减库存
            product.setStock(product.getStock() - item.getQuantity());
            productsMapper.update(product);
            // 生成订单
            Orders order = new Orders();
            order.setBuyerId(userId);
            order.setSellerId(product.getSellerId());
            order.setProductId(item.getProductId());
            order.setQuantity(item.getQuantity());
            order.setPrice(item.getProductPrice().multiply(new java.math.BigDecimal(item.getQuantity())));
            order.setStatus(1); // 1=待支付
            order.setOrderNumber(java.util.UUID.randomUUID().toString().replace("-", ""));
            order.setCreateTime(java.time.LocalDateTime.now());
            ordersMapper.insert(order);
        }
        // 批量逻辑删除购物车项
        shoppingCartsMapper.updateStatusBatch(cartItemIds, 1); // 1=已删除
        return true;
    }


}